package com.st.shopadmin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.shopadmin.common.ServerResponse;
import com.st.shopadmin.dao.ShopMapper;
import com.st.shopadmin.pojo.Shop;
import com.st.shopadmin.pojo.ShopUserPlat;
import com.st.shopadmin.service.ShopService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    public ServerResponse<PageInfo> getShopList(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shop> productList = shopMapper.selectList();
        PageInfo pageResult = new PageInfo(productList);
        return  ServerResponse.createBySuccess(pageResult);
    }

    public ServerResponse<List> getShopLists(int pageNum,int pageSize){
        /*List<Shop> productList = shopMapper.selectList();*/
        List<ShopUserPlat> productList = shopMapper.selectShopUserPlatList();
        return  ServerResponse.createBySuccess(productList);
    }

    public ServerResponse<List> searchLists(Shop shop,int pageNum,int pageSize){
        List<Shop> productList = shopMapper.searchLists(shop);
        return  ServerResponse.createBySuccess(productList);
    }

    public ServerResponse<List> newSearchLists(ShopUserPlat shop,int pageNum,int pageSize){
        List<ShopUserPlat> productList = shopMapper.newSearchLists(shop);
        return  ServerResponse.createBySuccess(productList);
    }

    public ServerResponse<Shop> getShopDetail(Long shopId){
        if(shopId == null){
            return  ServerResponse.createByErrorMessage("参数错误");
        }
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        if(shop == null){
            return  ServerResponse.createByErrorMessage("产品已下架或者删除");
        }
        return  ServerResponse.createBySuccess(shop);

    }

    @Transactional
    public  ServerResponse updateShopStatus(Long shopId,Integer status,String shopNoPassReasons){
        if(status == null || shopId == null){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        Shop shop = new Shop();
        shop.setShopId(shopId);
        shop.setStatus(status);
        shop.setShopNoPassReasons(shopNoPassReasons);
        int rowCount  = shopMapper.updateByPrimaryKeySelective(shop);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Transactional
    public  ServerResponse updateShop(Shop shop){
        if(shop == null ){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        int rowCount  = shopMapper.updateByPrimaryKeySelective(shop);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }
    @Transactional
    public ServerResponse shopSave(Shop shop){
        if(shop != null){
                int rowCount =  shopMapper.insert(shop);
                if(rowCount > 0){
                    return ServerResponse.createBySuccess("新增店铺成功");
                }else{
                    return ServerResponse.createByErrorMessage("新增失败");
                }
            }
        return ServerResponse.createByErrorMessage("新增或更新商品参数错误");
    }
}
